var appPath = getAppPath();

$(function(){
	initJspGrid();// 初始化加载视频模板列表
	
	// 检索日期范围
	$("#queryDateStart").datebox({
		formatter:dtFmt,
		parser:dtParser
	});
	$("#queryDateEnd").datebox({
		formatter:dtFmt,
		parser:dtParser
	});
	
	// 检索日期范围
	$("#dateStart").datebox({
		formatter:dtFmt,
		parser:dtParser
	});
	$("#dateEnd").datebox({
		formatter:dtFmt,
		parser:dtParser
	});
	
	$("#queryDesigner").combobox({
		url:appPath+'/itbCoupon/getOrgUserForCombo.action?blkFlg=1&orgId=1',
		valueField:'id',
		textField:'name',
		onSelect:function(selRecdU){
			// TODO
		}
	});
	
	// 排序
	$("#querySort").combobox({
		data : [{"id":0,"name":"默认排序"},{"id":1,"name":"买单次数"}],
		valueField : 'id',
		textField : 'name',
		onLoadSuccess:function(rel){
			$("#querySort").combobox('select',0);
		}
	});
	
	$('#playVideoDlg').window({
	       onBeforeClose:function(){ 
	    	   document.getElementById('videoInfo').pause();
	       }
	});
	$('#playUserVideoDlg').window({
		onBeforeClose:function(){ 
			document.getElementById('userVideoInfo').pause();
		}
	});
	
	
	initUploader();
});

function dtFmt(dt){
	var y = dt.getFullYear();
    var m = dt.getMonth()+1;
    var d = dt.getDate();
    return y+'-'+(m<10?('0'+m):m)+'-'+(d<10?('0'+d):d);//格式化mm-dd
}
function dtParser(s){
    if (!s) return new Date();
    var ss = (s.split('-'));
    var y = parseInt(ss[0],10);
    var m = parseInt(ss[1],10);
    var d = parseInt(ss[2],10);
    if (!isNaN(y) && !isNaN(m) && !isNaN(d)){
        return new Date(y,m-1,d);
    } else {
        return new Date();
    }
}

function playParse(){
	var myVideo = document.getElementsByTagName('video')[0];
	if (myVideo.paused){
		myVideo.play();
	} else{
		myVideo.pause();
   }
}


// 加载视频设计模板
function initJspGrid(){
	$('#tmplGrid').datagrid({
	    url:appPath + "/itbVideo/getVideoDesignInfo.action",
	    height:'full',
	    striped:true,
	    fitColumns:true,
	    nowrap: false,// 换行
	    queryParams: getQueryParam(),
	    singleSelect:false,
	    selectOnCheck:true,
	    checkOnSelect:true,
	    frozenColumns:[[
	        {field:'ck',checkbox:'true',width:10,align:'center',sortable:false}
	    ]],
	    columns:[[
  	        {field:'theme_thumb',title:'封面图',width:80,align:'center',sortable:false,
	        	formatter:function(value, rowData, rowIndex){
	        		var retUrl = '';
	        		// http://itbour-back.oss-cn-hangzhou.aliyuncs.com/image/U2/2018/08/24/115022627_K81zQJJ1g5hkTeBU0ZWs
	        		if (rowData.theme_thumb) {
	        			retUrl = '<a href="javascript:void(0)" onclick="playVideo('+rowData.design_id+',\''+rowData.theme_thumb+'\',\''+rowData.video+'\')">'
	        				+ '<div style="display:table;text-align:center;height:80px;width:100%">'
	        				+ '<span style="display:table-cell;vertical-align:middle">'
	        				+ '<img src="' +rowData.theme_thumb+'?x-oss-process=image/resize,l_100'
	        				+ '" style="display:inline;max-height:80px;max-width:110px"></span></div></a>';
	        		} else {
	        			retUrl = '<div style="display:table;text-align:center;height:80px;width:100%">'
        				+ '<span style="display:table-cell;vertical-align:middle">'
        				+ '<img src="http://itbour-back.oss-cn-hangzhou.aliyuncs.com/image/U2/2018/08/24/115022627_K81zQJJ1g5hkTeBU0ZWs?x-oss-process=image/resize,l_100'
        				+ '" style="display:inline;max-height:80px;max-width:110px"></span></div>';
	        		}
	        		return retUrl;
	        	}
  	        },
//	        {field:'edit',title:'编辑',width:25,align:'center',sortable:false,
//		    	formatter:function(id, rowData, rowIndex){
//		    		var ret = "<a href='javascript:void(0)'><img width='12px' height='12px' style='border:0px' src='"+appPath+"/js/lib/jquery-easyui/themes/icons/pencil.png' title='编辑' ";
//					ret += " onClick='editTmpl("+rowData.design_id+","+rowData.type_id+","+rowData.size_id+","+rowData.publish+")'/></a>";
//					return ret;
//		    	}
//		    },
		    {field:'design_id',title:'模板ID',width:35,align:'center',sortable:false,
		    	formatter:function(id, rowData, rowIndex){
		    		var ret = "<a href='javascript:void(0)'><img width='12px' height='12px' style='border:0px' src='"+appPath+"/js/lib/jquery-easyui/themes/icons/pencil.png' title='编辑' ";
					ret += " onClick='editTmpl("+rowData.design_id+","+rowData.type_id+","+rowData.size_id+","+rowData.publish+")'/></a>";
		    		var show = '<font style="color:red">'+id+'</font><br/>'+ret;
		    		return show;
		    	}
		    },
	        {field:'size',title:'尺寸',width:50,align:'center',sortable:false,
		    	formatter:function(value, rowData, rowIndex){
		    		return 'w:'+(rowData.width||'N/A')+
		    			'<br/>H:'+(rowData.height||'N/A')+'';
		    	}
		    },
		    {field:'user_id',title:'设计师',width:50,align:'left',sortable:false,
		    	formatter:function(value, rowData, rowIndex){
		    		var ret = '<span style="color:#EEE0E5;font-size:10px;">暂无</span>';;
		    		if(rowData.user_name){
		    			ret = rowData.user_name;
		    		}
		    		return ret;
		    	}
		    },
	        {field:'order',title:'排序',width:38,align:'center',sortable:false,
		    	  formatter:function(value, rowData, rowIndex){
		    		  if(!rowData.design_id) return "";
		    		  return "<a href='javascript:void(0)' onClick='editOrder("+rowData.design_id+","+rowData.order+")'>"+rowData.order+"</a>";
		    	  }
		    },
	        {field:'design_name',title:'模版名称',width:85,align:'center',sortable:false,
		    	formatter:function(value,rowData,rowIndex){
		    		if (value){
		    			return '<img width="13px" src="'+appPath+'/images/system/icon/cs-comment.png"'+
		    			'onclick="editDesName('+rowData.publish+','+rowData.design_id+',\''+value+'\')" /><br/>'+value;
//		    			r1eturn '<a href="javascript:void(0)" onclick="editDesName('+rowData.publish+','+rowData.design_id+',\''+value+'\')">'+value+'</a>';
		    		}
		    		return '-';
		    	}
	        },
	        {field:'usePayCnt',title:'买单次数',width:40,align:'center',sortable:false,
	        	formatter:function(value,rowData,rowIndex){
	        		if (value!=null){
	        			return '<span style="color:#EE4000;"><font size="3" face="verdana">'+value+'</font></span>';
	        		}
	        		return '<span><font size="3" face="verdana">0</font></span>';
	        	}
	        },
	        {field:'meal',title:'套餐',width:30,align:'center',sortable:false,
		    	formatter:function(value, rowData, rowIndex){
		    		if (rowData.price_id){
		    			return '<a href="javascript:void(0)" style="color:green;" onclick="showPrice('+rowData.uni_item_id+')">已选</a>';
		    		}
		    		return '<a href="javascript:void(0)" onclick="showPrice('+rowData.uni_item_id+')">选择</a>';
		    	}
		    },
	        {field:'keyword',title:'关键词',width:35,align:'center',sortable:false,
		    	formatter:function(value, rowData, rowIndex){
		    		if(!rowData.design_id) return "";
		    		var ret = "<a href='javascript:void(0)'><img width='12px' height='12px' style='border:0px' src='"+appPath+"/js/lib/jquery-easyui/themes/icons/pencil.png' title='设置' ";
					ret += " onClick='editKeyword("+rowData.design_id+")'/></a>";
					return ret;
		    	}
		    },
		    {field:'form_id',title:'表单',width:30,align:'center',sortable:false,
		    	formatter:function(value, rowData, rowIndex){
		    		if (value){
		    			return "<a href='javascript:void(0)' style='color:green;' onclick='showViForm("+rowData.uni_item_id+","+rowData.design_id+","+rowData.form_id+")'>已设</a>";
		    		}
		    		return "<a href='javascript:void(0)' onclick='showViForm("+rowData.uni_item_id+","+rowData.design_id+","+rowData.form_id+")'>设定</a>";
		    	}
		    },
	        {field:'vip',title:'VIP',width:45,align:'center',sortable:false,
		    	  formatter:function(value, rowData, rowIndex){
		    		  var show = '普通模版';
		    		  if(Number(value||'0')==10) show="<span style='color:red;'>VIP模版</span>";
		    		  return "<a href='javascript:void(0)' onClick='editVip("+rowData.design_id+","+rowData.vip+")'>"+show+"</a>";
		    	  }
		    },
        	{field:'state',title:'模版状态',width:40,align:'center',sortable:false,
	        	formatter:function(state, rowData, rowIndex){
	        		var s = Number(state||'0');
	        		var nm = '未知';
	        		switch(s){
	        			case 10: nm='<span style="color:#8B6914">制作中</span>';break;
	        			case 20: nm='<span style="color:blue">待审核</span>';break;
	        			case 31: nm='<span style="color:orange">初审OK</span>';break;
	        			case 41: nm='<span style="color:red">初审NG</span>';break;
	        			case 32: nm='<span style="color:green">复审OK</span>';break;
	        			case 42: nm='<span style="color:red">复审NG</span>';break;
	        		}
	        		return nm;
	        	}},
	        {field:'publish',title:'发布状态',width:40,align:'center',sortable:false,
	        	formatter:function(publish, rowData, rowIndex){
	        		if (Number(publish) == 1) return '<span style="color:green">已发布</span>';
	        		return '<span style="color:red">未发布</span>';
	        	}
	        },
	        {field:'shApply',title:'用户视频',width:40,align:'center',sortable:false,
	        	formatter:function(publish, rowData, rowIndex){
	        		var dcnt = Number(rowData.dcnt||'0');
	        		return '<a href="javascript:void(0)" onclick="shApply('+rowData.design_id+')">查看【'+dcnt+'】</a>';
	        	}
	        },
	        {field:'create_time',title:'创建时间',width:50,align:'center',sortable:false},
	        {field:'update_time',title:'更新时间',width:45,align:'center',sortable:false}
	    ]],
	    hideColumn:['user_type'],
	    pagination:true,
		rownumbers:true,
		toolbar:[{
			text:'发布模板',
			iconCls:'icon-ok',
			handler:function(){
				doModification(1);
			}
		},
		'-',
		{
			text:'取消发布',// <img width="13px" src="'+appPath+'/images/system/icon/cs-errcorrect.png"/>
			iconCls:'icon-cancel',
			handler:function(){
				cancelPublish(0);
			}
		},
		'-',
		{
			text:'删除模板',
			iconCls:'icon-no',
			handler:function(){
				delTmpl();
			}
		},'-'
		]
	    
	});
}

function editDesName(publish,designId,desName){
	if (publish === 1){// 表示已经发布的模板
		$.messager.confirm('确认','该模板为已发布状态,是否继续修改模板名称?',function(r){
			if (r){
				doEditDesName(publish,designId,desName);
			}
		});
	} else {
		doEditDesName(publish,designId,desName);
	}
}

function doEditDesName(publish,designId,desName){
	$("#editDesForm").form('clear');
	$("#editDesNameDesignId").val(designId);
	$("#editDesName").val(desName);
	$("#editDesNameBtn").show();
	$('#dialog').window({
		title:'修改视频模板的名称',
		iconCls:'icon-edit',
		modal: true,
		shadow: true,
		collapsible:false,
		minimizable:false,
		maximizable:false
	});
	$('#dialog').window('move',{top:20,left:20});
	$('#dialog').window('open');
}



// 播放视频资源
function playVideo(designId,thumb,videoUrl){
	
	$("#vDesignId").val(designId);
	$('#playVideoDlg').window({
		title:'视频播放',
		iconCls:'icon-tip',
		modal: true,
		shadow: true,
		collapsible:false,
		minimizable:false,
		maximizable:false
	});
	if (!videoUrl){
		return $.messager.alert('警告','该视频资源暂无..','warning');
	}
	
	$('#playVideoDlg').window('move',{top:20,left:200});
	$('#playVideoDlg').window('open');
	
	// 缩略图
	var imgHtml = '<a href="'+thumb+'" target="blank"><img src="'+thumb+'" style="display:inline;max-height:100px;max-width:200px"/></a>';
	$("#topicImg").html(imgHtml).val(thumb);
	
	
	// 视频 播放资源
	var htm = '<video style="margin-left:20px;height:270px;" id="videoInfo" controls="controls" preload="auto" poster="'+thumb+'" src="'+videoUrl+'"></video>';
	$("#viderUrl").html(htm);
}

function updVideoThumb(){
	var designId = $("#vDesignId").val();
	var imgUrl = $("#topicImg").val();
	$.messager.confirm('确定','是否确定更改视频封面图？',function(r){
		if (r){
			var url = appPath+"/itbWebVideo/upVideoDesignInfo.action?ids="+designId+"&theme_thumb="+imgUrl;
			$.post(url,function(data){
				if(data=="ok"){
					$.messager.alert('提示信息','保存成功','warning');
				} else {
					$.messager.alert('提示信息','保存失败','warning');
				}
			});
		}
	});
}

function initUploader(){
	var url = "http://upload.itbour.com/upload/user?device=101&user_type=0&user_id="+(g_userId||0);
    var option = {};
    option.id = 'selImgFile';
    option.url = url;
    option.callback = function(res){
    	$("#topicImg").html('');
    	if(!res.success){
            return;
    	}
    	if(res && res.data && res.data.url){
    		var showImg = '';
			if(res.data.url && res.data.url.length>0) {
				showImg = res.data.url+'?x-oss-process=image/resize,m_pad,h_150,w_300,color_cccccc';
			}
			var imgHtml = '<a href='+res.data.url+' target="blank"><img src="'+showImg+'" style="display:inline;max-height:100px;max-width:200px"/></a>';
			$("#topicImg").append(imgHtml).val(res.data.url||'');
			$("#videoInfo").attr("poster",showImg); // 某一个 元素设置属性
    	}
    }
    option.imgExts = ['jpg','jpeg','png'];
    var uploader = new AjaxUploadEx(option);
}

// 播放视频资源
function playUserDocVideo(thumb,videoUrl){
	$('#playUserVideoDlg').window({
		title:'视频播放',
		iconCls:'icon-tip',
		modal: true,
		shadow: true,
		collapsible:false,
		minimizable:false,
		maximizable:false
	});
	if (!videoUrl){
		return $.messager.alert('警告','该视频资源暂无..','warning');
	}
	
	$('#playUserVideoDlg').window('move',{top:20,left:200});
	$('#playUserVideoDlg').window('open');
	var retUrl = '<a href="'+thumb+'" target ="_blank" '
	+ '<div style="display:table;text-align:center;height:150px;width:100%;">'
	+ '<span style="display:table-cell;vertical-align:middle">'
	+ '<img src="' +thumb+'?x-oss-process=image/resize,m_lfit,h_146,w_250'
	+ '" style="display:inline;max-height:150px;max-width:400px"></span></div></a>';
	
	$("#userVideoThumb").html(retUrl);
	
	var htm = '<video style="margin-left:20px;height:270px;" id="userVideoInfo" controls="controls" preload="auto" poster="'+thumb+'" src="'+videoUrl+'"></video>';
	$("#userViderUrl").html(htm);
}


// 展示价格套餐
function showPrice(uni_item_id){
	$("#uitemId").val(uni_item_id);// 模板统一ID
	$('#mealTable').html('');
	if (uni_item_id==null || uni_item_id.length<=0){
		$.messager.alert('提示信息','该模板不能设定套餐信息！请联系管理员','warning');
		return;
	}
	
	var url = appPath+"/itbVideo/getVideoPriceMealInfo.action?uItemId="+uni_item_id;
	$.post(url,function(ret){	
		if(ret && ret.length>0){
			for(var i=0; i<ret.length; i++){
				var sub = ret[i];
				if(sub && sub.length>0){
					var itemHtml = '<tr><td class="queryTitle" width="100px">'+sub[0].type_name+'套餐</td>'+
							'<td class="queryContent" colspan="4">';
					for(var n=0; n<sub.length; n++){
						var chk = (Number(sub[n].uni_item_id||'0')>0)?"checked":"";
						itemHtml += '<div style="width:80px;float:left">'+
							'<input type="checkbox" '+chk+' id="price_'+sub[n].price_id+'" name="viPriceInfo" value="'+sub[n].price_id+'"/>'+
							sub[n].name+'</div>';
					}
					itemHtml += '</td></tr>';
					$('#mealTable').append(itemHtml);
				}
			}
		}
	});
	
	$("#mealBtn").show();
	$('#mealDlg').window({
		title:'视频模板套餐价格',
		iconCls:'icon-edit',
		modal: true,
		shadow: true,
		collapsible:false,
		minimizable:false,
		maximizable:false
	});
	
	$('#mealDlg').window('move',{top:20,left:20});
	$('#mealDlg').window('open');
}

function cancelMeal(){
	$('#mealDlg').window('close');
}


function showViForm(uni_item_id,designId,formId){
	$("#uitemId").val(uni_item_id);// 模板统一ID
	$("#designIdForm").html(designId);
	if (formId){
		$("#formId").html(formId);
	} else {
		$("#formId").html("暂未设置表单");
	}
	
	$("#formBtn").show();
	$('#viFormDlg').window({
		title:'视频表单套餐',
		iconCls:'icon-edit',
		modal: true,
		shadow: true,
		collapsible:false,
		minimizable:false,
		maximizable:false
	});
	$('#viFormDlg').window('move',{top:20,left:20});
	$('#viFormDlg').window('open');
	initViForm();
}

function cancelViForm(){
	$('#viFormDlg').window('close');
}

function initViForm(){
	$('#viFormTanle').datagrid({
	    url:appPath + "/yjxForm/getFormMgrInfo.action",
	    height:'full',
	    striped:true,
	    fitColumns:true,
	    singleSelect:true,
	    collapsible: true,
		checkbox:false,
	    frozenColumns:[[
	        {field:'ck',checkbox:'true',width:10,align:'center',sortable:false}
	    ]],
	    columns:[[
	        {field:'form_id',title:'表单ID',width:25,align:'center',sortable:false}, 
	        {field:'form_type',title:'表单类型',width:35,align:'center',sortable:true,
	        	formatter:function(value,rowData,rowIndex){
	        		var ret = 'Nan';
	        		switch(value){
	        		case 10: ret = '用户收集'; break;
	        		}
	        		return '<span style="color:#B22222;">'+ret+'</span>';
	        	}
	        },
	        {field:'key1',title:'项目1',width:25,align:'center',sortable:true},
	        {field:'key2',title:'项目2',width:25,align:'center',sortable:true},
	        {field:'key3',title:'项目3',width:25,align:'center',sortable:true},
	        {field:'key4',title:'项目4',width:25,align:'center',sortable:true},
	        {field:'key5',title:'项目5',width:25,align:'center',sortable:true},
	        {field:'key6',title:'项目6',width:25,align:'center',sortable:true},
	        {field:'key7',title:'项目7',width:25,align:'center',sortable:true},
	        {field:'key8',title:'项目8',width:25,align:'center',sortable:true},
	        {field:'key9',title:'项目9',width:25,align:'center',sortable:true},
	        {field:'key10',title:'项目10',width:25,align:'center',sortable:true},
	        {field:'desc',title:'描述',width:40,align:'center',sortable:true},
	        {field:'thumb',title:'缩略图',width:40,align:'center',sortable:true},
	        {field:'create_time',title:'创建时间',width:40,align:'center',sortable:true},
	        {field:'update_time',title:'修改时间',width:60,align:'center',sortable:true}
	    ]],
	    hideColumn:['user_type'],
	    pagination:true,
		rownumbers:false,
		toolbar:[{
		 			text:'给视频模板添加表单',
		 			iconCls:'icon-ok',
		 			handler:function(){
		 				addFormId();
		 			}
		 		}],
		onLoadSuccess: function(){
			$(this).datagrid('freezeRow',0).datagrid('freezeRow',1);
		}
	});
}

// 添加formId 到统一模板表
function addFormId(){
	var selectItem=$("#viFormTanle").datagrid("getChecked");
	if(!selectItem || selectItem.length == 0){
		$.messager.alert('提示信息','请选择设计模版','warning');
		return;
	}
	var unItemId=$("#uitemId").val();// 模板统一ID
	if(!unItemId || unItemId.length == 0){
		$.messager.alert('提示信息','该模板有问题,请联系管理员','warning');
		return;
	}
	$.messager.confirm("确认", "是否确认给视频模板添加表单！", function (r) {  
        if (r) { 
        	$.loading(true);
			var ids = "";
			$.each(selectItem, function(index, item){
				ids = item.form_id;
			});
			var url = appPath+"/itbWebVideo/addVideFormId.action?formId="+ids+"&unItemId="+unItemId;
			$.post(url,function(data){
				$.loading(false);
				if(data=="ok"){
					$.messager.alert('提示信息','添加表单成功','info');
				}else{
					$.messager.alert('提示信息','添加表单失败','info');
				}
				doQuery();
			});
        }
	});
}


/* 设置视频套餐
 */
function updMeal(){
	var priceId = $("input[name='viPriceInfo']:checked").map(function(){
        	return $(this).val();
        }).get(); //  获取priceId
	var uitemId = $("#uitemId").val();// 模板统一ID
	
	if (priceId.length>1){
		return $.messager.alert('提示信息','套餐价格只能选择一个!','warning');
	}
	
	$.messager.confirm("确认", "是否设置更新套餐？", function (r) {
		if(r){
			var url = appPath +"/itbWebVideo/addPriceRInfo.action";
			var data = {
					'uItemId':uitemId,
					'price_id':priceId.join(',')
			};
			$.post(url,data,function(ret){
				$('#mealDlg').window('close');
				if(ret == 'ok'){
					$.messager.alert('提示信息','设置套餐成功','info');
				}else{
					$.messager.alert('提示信息','设置套餐失败','warning');
				}
			});
		}
	});
	
}


/* 	View the Video template user application list
 */
function shApply(id){
	$("#udocForm").form('clear');
	$("#uDocTable").show();
	$("#disign_id").html(id);
	// 买单状态 
	$("#payState").combobox({
		data : [{"id":0,"name":"[不限]"},{"id":1,"name":"已买单"}],
		valueField : 'id',
		textField : 'name',
		onLoadSuccess:function(rel){
			$("#payState").combobox('select',0);
		}
	});
	
	// 用户属性
	$("#testFlg").combobox({
		data : [{"id":'',"name":"全部"},{"id":'0',"name":"真实用户"},{"id":'1',"name":"测试用户"}],
		valueField : 'id',
		textField : 'name',
		onLoadSuccess:function(){
			$(this).combobox('select','');
		}
	});
	
	
	$("#uDocDlg").window({
		title :'用户视频信息',
		iconCls :'icon-blank',
		modal :true,
		shadow :true,
		collapsible :false,
		minimizable :false,
		maximizable :false
	});
	$('#uDocDlg').window('move',{top:20,left:20});
	$('#uDocDlg').window('open');
	
	initVideoUserDoc(id);
}

/**	初始化用户视频表
 */
function initVideoUserDoc(id){
	$('#uDocGrid').datagrid({
	    url:appPath + "/itbVideo/getUserVideoDoc.action?disId="+id,
	    height:'full',
	    striped:true,
	    fitColumns:true,
	    queryParams: getQueryParam3(),
	    singleSelect:true,
	    selectOnCheck:true,
	    checkOnSelect:true,
	    columns:[[
			{field : 'thumbs',title : '缩略图',width : 30,align : 'center',sortable : false,
				formatter : function(value, rowData, rowIndex) {
					var retUrl = '';
					if (value){
						var video = rowData.video;
						var thumb = JSON.parse(value);
						if (thumb == null || thumb.length<=0) {
							thumb = 'http://itbour-user.oss-cn-hangzhou.aliyuncs.com/image/U2/2017/11/06/191334526_2eKu1A5k5LmGoc4fezKI.jpg';
						}
						retUrl = '<a href="javascript:void(0)" onclick="playUserDocVideo(\''+thumb+'\',\''+video+'\')">'
						+ '<div style="display:table;text-align:center;height:70px;width:100%;">'
						+ '<span align="center" style="display:table-cell;vertical-align:middle">'
						+ '<img src="'+thumb+'?x-oss-process=image/resize,m_fixed,h_60,w_100'
						+ '" style="display:inline;max-height:70px;max-width:70px;"/></span></div></a>';
					}
					return retUrl;
				}
			},
			{field:'doc_id',title:'作品ID',width:20,align:'center',sortable:false},
	        {field:'doc_name',title:'作品名称',width:50,align:'center',sortable:false,
  	        	formatter:function(value, rowData, rowIndex){
  	        		if(value){
  	        			return '<span style="color:	#555555;">'+value+'</span>';
  	        		}
	        		return '<span style="color:#B0B0B0;">暂无</span>';
	        	}
		    },
	        {field:'purpose',title:'作品分类',width:30,align:'center',sortable:false,
  	        	formatter:function(value, rowData, rowIndex){
  	        		var tem = '';
  	        		switch(Number(value||'0')){
	  	        		case 0: tem ='<span style="color:green;">普通文档</span>'; break;
	  	        		case 1: tem ='<span style="color:#FFBBFF;">测试用</span>'; break;
	  	        		case 2: tem ='<span style="color:blue;">印品订单</span>'; break;
  	        		}
  	        		return tem;
	        	}
		    },
		    {field :'user_id',title :'用户ID', width:25, align:'center',sortable:false,
		    	formatter:function(value,rowData,rowIndex){
		    		if(value){
		    			return '<span style="color:#555555;">'+value+'</span>';
		    		}
		    	}
		    },
		    {field :'nickname',title :'用户昵称', width:45, align:'center',sortable:false,
		    	formatter:function(value,rowData,rowIndex){
		    		var st = '';
		    		if (rowData.test_flg == 1){
		    			st = '<br/><font color="red">Test</font>';
		    		}
		    		
		    		if(value){
		    			return '<span style="color:#436EEE;">'+value+'</span>'+st;
		    		}
		    		return '<span style="color:#9C9C9C;">暂无</span>';
		    	}
		    },
		    {field :'mobile',title :'手机号', width:30, align:'center',sortable:false,
		    	formatter:function(value,rowData,rowIndex){
		    		return value || '<span style="color:#9C9C9C">暂未填写</span>';
		    	}
		    },
		    {field :'state',title :'买单状态', width:30, align:'center',sortable:false,
		    	formatter:function(value,rowData,rowIndex){
		    		if (value){
		    			return '<span style="color:red;">已买单</span>';
		    		}
		    		return '未买单';
		    	}
		    },
	        {field:'create_time',title:'创建时间',width:50,align:'center',sortable:false},
	        {field:'update_time',title:'更新时间',width:50,align:'center',sortable:false}
	    ]],
	    pagination:true,
		rownumbers:true,
		toolbar:[]
	});
}


/* 编辑设计模版
 */
function editTmpl(designId, typeId, sizeId, publish){
	if (publish == 1 || publish =="1") {
		$.messager.alert('提示信息','请勿编辑已发布设计模版！','warning');
	} else {
		// 非加密方式
		//var editorUrl = getDesignServer()+"/design/getWorkEditors?"+"uId="+g_userId+"&dId="+designId;
		// 加密方式
		var editorUrl = getDesignServer()+'/encrypt/encrypt'+urlWithPara({'uId':g_userId,'dId':designId,'typeId':typeId,'sizeId':sizeId,
				'destination':'/design/getVideoEditor'});
		window.open(editorUrl);
	}
}

/* 删除设计模版
 */
function delTmpl(){
	var selectItem=$("#tmplGrid").datagrid("getChecked");
	var ids = "";// 删除的模板ID
	var publish = '';// 模板发布状态
	var cnt = 0;
	
	if(!selectItem || selectItem.length == 0){
		$.messager.alert('提示信息','请选择视频模版','warning');
		return;
	} else{
		
		$.each(selectItem, function(index, item){
			ids = ids + item.design_id + ",";
			publish = item.publish;
			cnt++;
		});
		console.log(ids);
		if (cnt>1) {
			$.messager.alert('提示信息','请单选删除视频模板','warning');
			return;
		} else {
			if (publish == 1){
				$.messager.alert('提示信息','目前只能删除[未发布]的模板','warning');
				return;
			}
		}
	}
	
	$.messager.confirm("确认", "是否确认删除", function (r) {  
        if (r) { 
        	$.loading(true);
			var url = appPath+"/itbWebVideo/upVideoDesignInfo.action?ids="+ids+"&del=1";
			$.post(url,function(data){
				if(data=="ok"){
					$.loading(false);
					$.messager.alert('提示信息','删除成功','info');
					doQuery();
				} else{
					$.loading(false);
					$.messager.alert('提示信息','删除失败','info');
				}
			});
        }
	});
}

/**
 * 显示顺序
 */
function editOrder(designId, order){
	$("#designId").val(designId);
	$("#order").val(order);
	$("#orderBtn").show();
	$('#orderDlg').window({
		title:'显示顺序设定',
		iconCls:'icon-edit',
		modal: true,
		shadow: true,
		collapsible:false,
		minimizable:false,
		maximizable:false
	});
	$('#orderDlg').window('move',{top:20,left:20});
	$('#orderDlg').window('open');
}
/**
 * 修改显示顺序
 */
function doUpdOrder(){
	var designId = $("#designId").val();
	var order = $("#order").val();
	if(!order || order.length == 0 || Number(order)>2000 || Number(order)<=0){
		$.messager.alert('提示信息','有效数值范围为：1～2000','warning');
		return;
	}
	
	var url = appPath+"/itbWebVideo/upVideoDesignInfo.action?ids="+designId+"&order="+order;
	$.post(url,function(data){
		if(data=="ok"){
			$.messager.alert('提示信息','显示顺序已更新！','info',function(){
				doQuery();
				$('#orderDlg').window('close');
			});
		}else {
			$.messager.alert('提示信息','显示顺序更新失败！','warning');
		}
	});
}
function doUpDesName(){
	var designId = $("#editDesNameDesignId").val();
	var desName = $("#editDesName").val();
	var url = appPath+"/itbWebVideo/upVideoDesignInfo.action?ids="+designId+"&desName="+desName;
	$.post(url,function(data){
		if(data=="ok"){
			$.messager.alert('提示信息','更新成功','info',function(){
				doQuery();
				$('#dialog').window('close');
			});
		}else {
			$.messager.alert('提示信息','更新失败','warning');
		}
	});
}
/**
 * 结束显示顺序编辑
 */
function cancelUpdOrder(){
	$('#orderDlg').window('close');
}
function cancelUpDesName(){
	$('#dialog').window('close');
}

/** 视频设计费用
 */
function editPrice(designId, price){
	$("#pDId").val(designId);
	$("#updPrice").val(price);
	
	$("#priceBtn").show();
	$('#priceDlg').window({
		title:'设计费设定',
		iconCls:'icon-edit',
		modal: true,
		shadow: true,
		collapsible:false,
		minimizable:false,
		maximizable:false
	});
	
	$('#priceDlg').window('move',{top:20,left:20});
	$('#priceDlg').window('open');
}


/*  修改视频 设计费
 */
function doUpdPrice(){
	var designId = $("#pDId").val();
	var price = Number($("#updPrice").val()||'0');
	if(isNaN(price) || price<0){
		$.messager.alert('提示信息','请输入有效数值','warning');
		return;
	}
	
	var url = appPath+"/itbWebVideo/upVideoDesignInfo.action?ids="+designId+"&price="+price;
	$.post(url,function(data){
		if(data=="ok"){
			$.messager.alert('提示信息','设计费已更新！','info',function(){
				doQuery();
				$('#priceDlg').window('close');
			});
		}else {
			$.messager.alert('提示信息','设计费更新失败！','warning');
		}
	});
}

/* 结束设计费编辑
 */
function cancelUpdPrice(){
	$('#priceDlg').window('close');
}

// 设定vip模版
function editVip(designId, vip){
	$("#vDId").val(designId);
	$("#updVip").combobox('select',Number(vip||'0')||0);
	$("#vipBtn").show();
	$('#vipDlg').window({
		title:'VIP模版设定',
		iconCls:'icon-edit',
		modal: true,
		shadow: true,
		collapsible:false,
		minimizable:false,
		maximizable:false
	});
	
	$('#vipDlg').window('move',{top:20,left:20});
	$('#vipDlg').window('open');
}

/*
 * 修改视频设计模板 VIP状态
 */
function doUpdVip(){
	var designId = $("#vDId").val();
	var vip = $("#updVip").combobox('getValue');
	if((Number(designId||'0')||0) <= 0) {
		$.messager.alert('提示信息','无效设计模版ID!','warning');
		return;
	}
	
	var url = appPath+"/itbWebVideo/upVideoDesignInfo.action?ids="+designId+"&vip="+vip;
	$.post(url,function(data){
		if(data=="ok"){
			$.messager.alert('提示信息','vip属性已更新！','info',function(){
				doQuery();
				$('#vipDlg').window('close');
			});
		}else {
			$.messager.alert('提示信息','vip属性更新失败！','warning');
		}
	});
}
function cancelUpdVip(){
	$('#vipDlg').window('close');
}

/* 	发布视频模板
 */
function doModification(flg){
	var selectTmpl=$("#tmplGrid").datagrid("getChecked");
	if(!selectTmpl || selectTmpl.length == 0){
		$.messager.alert('提示信息','请选择未发布设计模版进行处理！','warning');
		return;
	}else if(selectTmpl.length>1){
		$.messager.alert('提示信息','请选择单个设计模版进行处理！','warning');
		return;
	}	
	
	var ids = "";
	var doFlg = false;
	$.each(selectTmpl, function(index, item){
		if(item.publish != 1 && item.publish != '1' && !doFlg){
			doFlg = true;
			ids = item.design_id;
		}
	});
	if(!doFlg){
		$.messager.alert('提示信息','请选择未发布设计模版进行处理！','warning');
		return;
	}
	$.messager.confirm("确认",
		"是否确认发布设计模版？<br/><span style='color:red'>#请确保已经过认真检查</span>",
		function (r) {
	        if (r) {
	        	$.loading(true);
	        	var url = appPath+"/itbWebVideo/upVideoDesignInfo.action?ids="+ids+"&publish="+flg;
	        	$.post(url,function(data){
	        		if(data=="ok"){
	        			$.loading(false);
	        			doQuery();
	        			$.messager.alert('提示信息','发布成功','info');
	        		} else {
	        			$.loading(false);
	        			$.messager.alert('提示信息','发布失败','warning');
	        		}
	        	});
	        }
		}
	);
}


/** 取消发布
 */
function cancelPublish(flg){
	var selectTmpl=$("#tmplGrid").datagrid("getChecked");
	if(!selectTmpl || selectTmpl.length == 0){
		$.messager.alert('提示信息','请选择已发布设计模版进行处理！','warning');
		return;
	}else if(selectTmpl.length>1){
		$.messager.alert('提示信息','请选择单个模版进行处理！','warning');
		return;
	}
	
	var ids = "";
	var uniItemIds = ""; // 统一模板ID
	var doFlg = false;
	$.each(selectTmpl, function(index, item){
		if(item.publish == 1 || item.publish == '1'){
			doFlg = true;
			ids = item.design_id;
			uniItemId = item.uni_item_id;
		}
	});
	
	if(!doFlg){
		$.messager.alert('提示信息','请选择已发布设计模版进行处理！','warning');
		return;
	}
	
	$.messager.confirm("确认", "是否确认取消已发布设计模版？", function (r) {
		if(r){
			$.loading(true);
        	var url = appPath+"/itbWebVideo/upVideoDesignInfo.action?ids="+ids+"&publish="+flg+"&uniItemId="+uniItemId;
			$.post(url,function(data){
				if(data=="ok"){
					$.loading(false);
        			doQuery();
        			$.messager.alert('提示信息','取消发布成功','info');
				} else {
					$.loading(false);
					$.messager.alert('提示信息','取消发布失败','info');
				}
			});
		}
	});
}


/* 	 视频模板 列表参数
 */
function getQueryParam(){
	var queryObject = {};
		
	var findVip = $("#queryVip").combobox('getValue');
	var findPubFlg = $("#queryPublish").combobox('getValue');
	var findTmplName=$("#queryTmplName").val();
	var findTmplId=$("#queryTmplId").val();
	var findItemId=$("#queryItemId").val();
	var findUserId = Number($("#queryDesigner").combobox('getValue')||'0')||0;
	
	var findDtS = $("#queryDateStart").datebox('getValue');//获取开始结束日期
	var findDtE = $("#queryDateEnd").datebox('getValue');
	
	var findSort = $("#querySort").combobox('getValue');
	if (Number(findSort)>0) queryObject.findSort = findSort;
	
	if(findVip&&findVip.length>0) queryObject.findVip = findVip;// vip
	if(findPubFlg&&findPubFlg.length>0) queryObject.findPubFlg = findPubFlg;// 发布标记
	if(findTmplName&&findTmplName.length>0) queryObject.findTmplName = findTmplName;// 模版名称
	if(findTmplId&&findTmplId.length>0) queryObject.findTmplId = findTmplId;// 模版id
	if(findItemId&&findItemId.length>0) queryObject.findItemId = findItemId;// 商品id
	if(findUserId>0) queryObject.findUserId = findUserId;// 作者id
	if(findDtS && findDtS.length>0) queryObject.findDateStart = findDtS +' 00:00:00 ';
	if(findDtE && findDtE.length>0) queryObject.findDateEnd = findDtE+' 23:59:59';
	
	return queryObject;
}

/*	 查询数据
 */
function doQuery(startFlg){
	$("#tmplGrid").datagrid('unselectAll');
	if(startFlg == 1) {
		$("#tmplGrid").datagrid('load', getQueryParam());
	} else {
		$("#tmplGrid").datagrid('reload', getQueryParam());
	}
}

// 	视频用户文档
function doQuery3(){
	$("#uDocGrid").datagrid('unselectAll');
	$("#uDocGrid").datagrid('load', getQueryParam3());
}

function getQueryParam3(){
	var obj = {};
	var docId = $("#queryDocId").val();
	var userId = $("#queryUserId").val();
	var mobile = $("#queryMobile").val();
	
	var findState = $("#payState").combobox('getValue');
	var findTestFlg = $("#testFlg").combobox('getValue');
	
	if (Number(findState)>0) obj.findState = findState;
	if (Number(findTestFlg)>=0) obj.findTestFlg = findTestFlg;
	if(Number(docId)>0) obj.findDocId = docId;
	if(Number(userId)>0) obj.findUserId = userId;
	if(Number(mobile)>=0){
		obj.findMobile = mobile;
	}else{
		$.messager.alert('提示信息','请输入有效数字!','info');
	} 
	
	var findDtS = $("#dateStart").datebox('getValue');//获取开始结束日期
	var findDtE = $("#dateEnd").datebox('getValue');
	if(findDtS && findDtS.length>0) obj.findDateStart = findDtS +' 00:00:00 ';
	if(findDtE && findDtE.length>0) obj.findDateEnd = findDtE+' 23:59:59';
	
	return obj;
}

function fmtCreateTime(value, rowData, rowIndex){
	if(!value)return "";
	var s = new Date(value);
	return rowData.create_time=$.DateUtil.format(s,'yyyy/MM/dd hh:mm:ss');
}

//关键词窗口
function editKeyword(dId,topType){
	$("#kwDId").val(dId);
	$('#keywordTable').html('');
	
	var url = appPath+"/itbVideo/getKeywordLst.action?keyDesignId="+dId;
	$.post(url,function(ret){	
		if(ret && ret.length>0){
			for(var i=0; i<ret.length; i++){
				var sub = ret[i];
				if(sub && sub.length>0 && sub[0].type!=10){
					var itemHtml = '<tr><td class="queryTitle" width="80px">'+sub[0].type_name+'标签</td>'+
							'<td class="queryContent" colspan="4">';
					for(var n=0; n<sub.length; n++){
						var chk = (Number(sub[n].doc_id||'0')>0)?"checked":"";
						itemHtml += '<div style="width:80px;float:left">'+
							'<input type="checkbox" '+chk+' id="kwId_'+sub[n].key_id+'" name="keywordInfo" value="'+sub[n].key_id+'"/>'+
							sub[n].key_name+'</div>';
					}
					itemHtml += '</td></tr>';
					$('#keywordTable').append(itemHtml);
				}
			}
			
			$('#keywordTable').append('<tr><td class="queryTitle" width="80px">自定义标签</td>'+
					'<td class="queryContent" width="60px"><input class="inputText" type="text" id="myKwName_1"/></td>'+
					'<td class="queryContent" width="60px"><input class="inputText" type="text" id="myKwName_2"/></td>'+
					'<td class="queryContent" width="60px"><input class="inputText" type="text" id="myKwName_3"/></td>'+
					'<td class="queryContent" width="60px"><input class="inputText" type="text" id="myKwName_4"/></td>'+
					'</tr>');

			for(var i=0; i<ret.length; i++){
				var sub = ret[i];
				if(sub && sub.length>0 && sub[0].type==10){
					var cnt=1;
					for(var n=0; n<sub.length; n++){
						if(cnt<=4){
							$('#myKwName_'+cnt).val(sub[n].key_name);
							cnt+=1;
						}
					}
					break;
				}
			}
		}
	});
	
	$("#keywordBtn").show();
	$('#keywordDlg').window({
		title:'视频关键词/标签',
		iconCls:'icon-edit',
		modal: true,
		shadow: true,
		collapsible:false,
		minimizable:false,
		maximizable:false
	});
	
	$('#keywordDlg').window('move',{top:20,left:20});
	$('#keywordDlg').window('open');
}


/* 	设定视频关键词
 */
function updKW(){
	var defKeys = $("input[name='keywordInfo']:checked").map(function(){
        	return $(this).val();
        }).get(); // 获取关键词ID
	var userKeys = [];// 自定义关键词
	for(var i=1; i<=4; i++){
		var keyNm = $('#myKwName_'+i).val();
		if(keyNm && keyNm.length>0) userKeys.push(keyNm);
	}
	var dId = $("#kwDId").val();// 模板ID
	var topType = 1020;// 视频type_id
	
	$.messager.confirm("确认", "是否确认更新关键词？", function (r) {
		if(r){
			var url = getDesignServer()+'/encrypt/encrypt';
			var data = encryptParas({
				'type':topType,
				'uId':g_userId,
				'dId':dId,
				'defKeys': defKeys,
				'userKeys': userKeys,
				'destination': '/user/updDocKw'
			});
			$.post(url,data,function(ret){
				if(ret.success == 1){
					$.messager.alert('提示信息','关键词标签已更新！','info');
				}else{
					$.messager.alert('提示信息',(ret.msg||'更新失败'),'info');
				}
			});
		}
	});
	
}

function cancelKW(){
	$('#keywordDlg').window('close');
}
